package com.chenfan.controller;

import com.chenfan.pojo.Book;
import com.chenfan.pojo.PageBean;
import com.chenfan.pojo.Result;
import com.chenfan.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Slf4j
@RequestMapping("/books")
public class BookController {
    @Autowired
    private BookService bookService;
    /**
     * 分页查询
     */
    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize
                       ){
        log.info("分页查询, 参数: {},{}",page,pageSize);
        //调用service分页查询
        PageBean pageBean = bookService.page(page,pageSize);
        return Result.success(pageBean);
    }
    /**
     * 根据id删除
     */
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        log.info("删除");
        bookService.delete(ids);
        return Result.success();
    }
    /**
     * 新增图书
     */
    @PostMapping
    public Result add(@RequestBody Book book) {
        log.info("新增图书，{}",book);
        bookService.add(book);
        return Result.success();
    }

    /**
     * 根据id查询图书
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("根据id查询图书{}",id);
        Book book=bookService.findById(id);
        return Result.success(book);
    }

    /**
     * 更改图书
     * @param book
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Book book) {
        log.info("更改图书{}",book);
        bookService.update(book);
        return Result.success();
    }
}
